# -*- coding:utf-8 -*-
"""
1000以内算数表达式计算：输入中文计算公式，返回中文结果如 输入 ‘三十六加六十四’，返回结果‘一百’
1.初级：只有加减法计算 
2.中级：加减乘除法计算
3.高级：含括号加减乘除法计算

ps: python2.7环境
"""
# 思路：中文表达式转换为数学表达式字符串；再用eval函数计算；返回数字结果再转换为中文结果

# 将中文计算公式转换为数学计算表达式字符串
# 1.先将操作符号‘加-减-乘以-除以-左括号-右括号’转换为数学操作符‘+-*/()’
# 2.以数学操作符将中文字符串分割为按顺序的汉字数字、操作符号列表
# 3.将列表中的汉字数字成员转换为数学float型数字
# 4.将列表合并成一个数学字符串
# 5.eval计算
# 6.返回数字结果转换为中文数字


def sp_formula(formula_str):

    # 将汉语操作符转换为数学操作符号，将操作符统一为一个字符
    formula_str = formula_str.replace(u'加', u'+').replace(u'减', u'-').replace(u'乘以', u'*').replace(u'除以', u'/').replace(u'左括号', u'(').replace(u'右括号', u')')
    formula = []
    formulax = {}

    # 字符串序列最好的操作是操作偏移量，所以将字符串中的数学操作符提取出来，按在字符串中的{偏移量：操作符号}为成员组成字典
    idx = 0
    le = len(formula_str)
    while idx < le:
        var = formula_str[idx]
        if var in (u'+', u'-', u'*', u'/', u'(', u')'):
            formulax[idx] = var
        idx = idx + 1

    print formulax

    # 根据操作符偏移量(按顺序)分解字符串形成列表    
    key_sp = formulax.keys()
    key_sp.sort()
    idx_key = 0
    le_key = len(key_sp)

    while idx_key < le_key:
        if idx_key == 0:
            x = formula_str[0:key_sp[0]]
            if x != u'':
                formula.append(x)

        formula.append(formulax[key_sp[idx_key]])

        if idx_key == le_key-1:
            x = formula_str[key_sp[idx_key]+1 : None]
        else:
            x = formula_str[key_sp[idx_key]+1 : key_sp[idx_key+1]]
        
        if x != u'':
            formula.append(x)

        idx_key = idx_key + 1

    print formula
    
    # 遍历列表中的成员，对汉语数字成员进行转换
    idx_str = 0
    le_formula = len(formula)
    while idx_str < le_formula:
        num = formula[idx_str]
        if num not in (u'+', u'-', u'*', u'/', u'(', u')'):
            formula[idx_str] = ts_zh2num(num)
        idx_str = idx_str + 1

    return ''.join(formula)

# 数字转化为汉字
def ts_zh2num(rs_num):
    # 首先将汉字西文化，统一为一个字节，有利后面操作
    num = rs_num.replace(u'一', u'1').replace(u'二', u'2').replace(u'三', u'3').replace(u'四', u'4').replace(u'五', u'5').replace(u'六', u'6').replace(u'七', u'7').replace(u'八', u'8').replace(u'九', u'9')
    num = num.replace(u'千', u'q').replace(u'百', u'b').replace(u'十', u's').replace(u'零', u'l')

    # 注意此处是怎么实现的，代码是实现解决问题的方案的，代码不能代替思考！！！
    # 罗列出十五种汉语数字情况，可以观察到汉语数字是具有层层包含关系的:
    # a.千中可以是包含百的，不包含百但包含十的，不包含百、十但包含个的，百十个都不包含的
    # b.百中可以是包含十的，不包含十但包含个的，十、个都不包含的
    # c.十中是可以包含个的，不包含个的
    # d.个、零
    # 所以这是一个包含的关系，可以从此处入手，避免用十五个if-else来
    def g(num):
        if num == u'l':
            r = 0.0
        else:
            r = float(num)
        return r

    def s(num):
        if not num.endswith(u's'):
            if num.startswith(u's'):
                r = 1.0 * 100 + g(num[1:None])
            else:
                r = float(num[0:1]) * 10 + g(num[2:None])
        else:
            if num.startswith(u's'):
                r = 1.0 * 10
            else:
                r = float(num[0:1]) * 10
        return r

    def b(num):
        if u's' in num:
            r = float(num[0:1]) * 100 + s(num[2:None])
        elif u's' not in num and u'l' in num:
            r = float(num[0:1]) * 100 + g(num[3:None])
        else:
            r = float(num[0:1]) * 100
        return r

    def q(num):
        if u'b' in num:
            r = float(num[0:1]) * 1000 + b(num[2:None])
        elif u'b' not in num and u's' in num:
            r = float(num[0:1]) * 1000 + s(num[3:None])
        elif u'b' not in num and u's' not in num and u'l' in num:
            r = float(num[0:1]) * 1000 + g(num[3:None])
        else:
            r = float(num[0:1]) * 1000
        return r

    if u'q' in num:
        nm = q(num)
    elif u'q' not in num and u'b' in num:
        nm = b(num)
    elif u'q' not in num and u'b' not in num and u's' in num:
        nm = s(num)
    elif u'q' not in num and u'b' not in num and u's' not in num:
        nm = g(num)

    return u'%f' % (nm)

# 将数字转换为中文，方案参照中文转数字，利用包含关系转换
def ts_num2zh(num):
    def g(num):
        if num == 0:
            z = u'零'
        elif num == 1:
            z = u'一'
        elif num == 2:
            z = u'二'
        elif num == 3:
            z = u'三'
        elif num == 4:
            z = u'四'
        elif num == 5:
            z = u'五'
        elif num == 6:
            z = u'六'
        elif num == 7:
            z = u'七'
        elif num == 8:
            z = u'八'
        elif num == 9:
            z = u'九'
        return z

    def s(num):
        if num%10 > 0:
            z = u'%s十%s' % (g(num//10), g(num%10))
        elif num%10 == 0:
            z = u'%s十' % (g(num//10))
        return z

    def b(num):
        if num%100 >= 10:
            z = u'%s百%s' % (g(num//100), s(num%100))
        elif num%100 < 10 and num%100 != 0:
            z = u'%s百零%s' % (g(num//100), g(num%100))
        elif num%100 == 0:
            z = u'%s百' % (g(num//100))
        return z

    def q(num):
        if num%1000 >= 100:
            z = u'%s千%s' % (g(num//1000), b(num%1000))
        elif num%1000 < 100 and num%1000 >= 10:
            z = u'%s千零%s' % (g(num//1000), s(num%1000)) 
        elif num%1000 < 100 and num%1000 < 10 and num%1000 != 0:
            z = u'%s千零%s' % (g(num//1000), g(num%1000))
        elif num%1000 == 0:
            z = u'%s千' % (g(num//1000))
        return z
    print num//1000 > 0, num//1000 == 0 and num//100 > 0, num//1000 == 0 and num//100 == 0 and num//10 > 0, num//10 == 0
    if num//1000 > 0:
        zh = q(num)
    elif num//1000 == 0 and num//100 > 0:
        zh = b(num)
    elif num//1000 == 0 and num//100 == 0 and num//10 > 0:
        zh = s(num)
    elif num//10 == 0:
        zh = g(num)
    return zh

# 进行加减法计算
def cal_ss(formula_str):
    formula = sp_formula(formula_str)
    
    # 利用解释性语言特有的eval函数来进行计算
    return int(eval(formula))

    #rs_num = cal_procc()

    #return ts_zh2num(rs_num) 





def cal_ssmd():
    pass

def cal_ssmd_b():
    pass

def start():
    while True:
        print '*'*12 + u'汉语计算器' + '*'*12
        print u'1.初级：只有加减法计算\n2.中级：加减乘除法计算\n3.高级：含括号加减乘除法计算\n4.退出'
        choose = raw_input(u'请输入您要进行的计算类型：')
        tip = u'请输入您要计算的公式：'
        if choose == '1':
            rs = cal_ss(raw_input(tip).decode('utf-8'))
        elif choose == '2':
            rs = cal_ssmd(raw_input(tip).decode('utf-8'))
        elif choose == '3':
            rs = cal_ssmd_b(raw_input(tip).decode('utf-8'))
        elif choose == '4':
            break
        else:
            pass
        if rs != None:
            print u'等于 %s' % ts_num2zh(rs)




if __name__ == '__main__':
    start()
else:
    pass